﻿/*
Customizer by Hoai Thanh Trinh
*/
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using FShop.BAL;
using FShop.DAL;
public partial class ManageAdvertise : System.Web.UI.Page 
{      
    protected void Page_Load(object sender, EventArgs e)
    {
        if ( ! IsPostBack )
        {
           
            BindDataGrid(1);
        }   
        else
        {
			
        } 
    }
	#region Bind data to grid
    protected void BindDataGrid(int pageIndex)
    {
		try
		{
            //grdAdvertise.DataSource = Advertise_BAL.Get_AdvertiseAsPaging(pageIndex, 10, "", "*", "", "");        
            grdAdvertise.DataSource = Advertise_BAL.GetList_Advertise();
			grdAdvertise.DataBind();				
		}
		catch(Exception ex)
        {
			throw ex;
        }
		
    } 
	#endregion		
	
	#region Delete Advertise
	protected void btnDeleteAll_Click(object sender, EventArgs e)
    {
		try{
			string s = AdvertiseSelectedArray();
			if (string.IsNullOrEmpty(s)) return;
			
            
            //xoa anh
            DataTable lst = Advertise_BAL.Get_AdvertiseAsTable("Adv_ID In (" + s + ")", "");
            if (lst == null || lst.Rows.Count == 0) return;
            
            string _sFolder = Request.PhysicalApplicationPath.Replace(@"\", "/") + "/Images/Adv/";
            foreach (DataRow r in lst.Rows)
            {
                ExtendFunctional.DeleteImageInFolder(_sFolder + "/" + r["Adv_Image"].ToString());                
            }

            Advertise_BAL.Delete_Advertise_Where("Adv_ID In (" + s + ")");
			MessageBox.Show("Đã xóa thành công.");
			BindDataGrid(1);
        }
        catch(Exception ex)
        {
			throw ex;
        }
    }
	//Lay ID tren cac cot duoc checked	
    private string AdvertiseSelectedArray()
    {
        string strArrayID = "";
        foreach (GridViewRow rows in grdAdvertise.Rows)
        {
            CheckBox chk = (CheckBox)rows.FindControl("chkCheck");

            if (chk.Checked)
            {
                ImageButton btnDel = (ImageButton)rows.FindControl("btnDelete");
                if (strArrayID != "")
                {
                    strArrayID += ",";
                }
                strArrayID += btnDel.CommandArgument.Trim();
            }
        }
        return strArrayID;
    }
	
	#endregion
	
	#region Datagrid events
	protected void grdAdvertise_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdAdvertise.PageIndex = e.NewPageIndex;
        BindDataGrid(grdAdvertise.PageIndex);

    }
    protected void grdAdvertise_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string strAdvertiseID = e.CommandArgument.ToString().Trim();
        string strCommand = e.CommandName;                                   
        switch (strCommand)
        {             
            
            case "delete_Advertise":
                try
                {
					Advertise advertise = Advertise_BAL.Get_Advertise_ByID(int.Parse(strAdvertiseID));
                    if (advertise == null) return;
                    Advertise_BAL.Delete_Advertise(advertise);

                    string _sFolder = Request.PhysicalApplicationPath.Replace(@"\", "/") + "/Images/Adv/";
                    ExtendFunctional.DeleteImageInFolder(_sFolder + "/" + advertise.Adv_Image);
                    MessageBox.Show("Đã xóa thành công.");
                    BindDataGrid(1);
                }
                catch(Exception ex)
				{
					throw ex;
				}
                break;
            case "active_Advertise":
                try
                {
                    Advertise advertise = Advertise_BAL.Get_Advertise_ByID(int.Parse(strAdvertiseID));
                    if (advertise == null) return;
                    advertise.Adv_Status = !advertise.Adv_Status;
                    Advertise_BAL.Update_Advertise(advertise);
                    MessageBox.Show("Đã kích hoạt thành công.");
                    BindDataGrid(1);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                break;   
        }
        
    }
	
	protected void grdAdvertise_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton imgActive = (ImageButton)e.Row.FindControl("btnActive");
            if (imgActive != null)
            {
                object obj = DataBinder.Eval(e.Row.DataItem, "Adv_Status");
                if (obj != null)
                {
                    if (obj.ToString().ToLower() == "true")
                    {
                        imgActive.ImageUrl = "Images/check.gif";
                    }
                    else
                    {
                        imgActive.ImageUrl = "Images/uncheck.gif";
                    }
                }
            }
        }
    }
	#endregion
	
	#region Extend events/codes
	
	#endregion
}


